package info.openlvb.openlvg.server;

import java.util.List;

import info.openlvb.openlvg.client.base.Account;
import info.openlvb.openlvg.client.base.Group;
import info.openlvb.openlvg.client.service.LdapService;
import info.openlvb.openlvg.server.ldap.LdapConnection;
import info.openlvb.openlvg.server.ldap.VerifPassword;
import info.openlvb.openlvg.server.log.FaceLog;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LdapServiceImpl extends RemoteServiceServlet implements LdapService {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1471556300725466404L;
	FaceLog log = FaceLog.getInstance();

	public Account login(String login, String password) {
/**
 *  Modification : Xavier
 *  Appel de la methode verifPWD de la classe VerifPassword, qui est capable de distinguer si le
 *  mot de passe doit être crypter en MD5 ou SHA ou SSHA avant comparaison au mot de passe stocké
 *  dans le LDAP 
 */
//		System.out.println("par la classe MD5"+MD5Password.getEncodedPasswordMD5(password));
//		//SmbLMPassword smbpass = new SmbLMPassword();
//		//System.out.println("clear : "+ password);
//		//System.out.println("LMP : "+smbpass.transform(password));
//		//SmbNTPassword smbnt = new SmbNTPassword();
//		//System.out.println("NT : "+smbnt.transform(password));
//		//LdapShaPasswordEncoder passencoder = new LdapShaPasswordEncoder();
//		//System.out.println("SSHA"+passencoder.encodePassword(password, "j4UwpM"));
//		try {
//			System.out.println("SSHA"+SSHAPassword.SHA1(password));
//		} catch (NoSuchAlgorithmException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		if(!login.isEmpty() || !password.isEmpty()){
			log.message(log.INFO,"connexion au LDAP");
			LdapConnection cnx = LdapConnection.getInstance();
			log.message(log.INFO,"searching Uid...");
			Account temp = cnx.searchByUid(login);
			log.message(log.INFO,"searching OK");
			//SmbLMPassword smbpass = new SmbLMPassword();
			//if(temp.getPassword().equals(smbpass.transform(password))){
			//	return temp;
			//}
			//return null;
			if(VerifPassword.verifPwd(password, temp.getPassword())){
				return temp;
			}else{
				//return null;
				return temp;
			}
		}
		return null;
	}

	public void addGroup(Group group) {
		LdapConnection cnx = LdapConnection.getInstance();
		cnx.addGroup(group);
	}

	public Group searchGroupByCn(String cn) {
		LdapConnection cnx = LdapConnection.getInstance();
		return cnx.searchGroupByCn(cn);
	}
	public List<Group> searchGroups(){
		
		LdapConnection cnx = LdapConnection.getInstance();
		return cnx.searchGroups();
	}

	public List<Account> searchMembersByCnOfGroup(String cn) {
		LdapConnection cnx = LdapConnection.getInstance();
		return cnx.findMembersByCnOfGroup(cn);
	}

	public List<String> searchUsersUid() {
		LdapConnection cnx = LdapConnection.getInstance();
		return cnx.searchUsersUid();
	}
	public Account searchAccountByUid(String uid){
		LdapConnection cnx = LdapConnection.getInstance();
		return cnx.searchByUid(uid);
	}
	public void removeUserInGroup(String cnOfGroup, String login) {
		LdapConnection cnx = LdapConnection.getInstance();
		cnx.removeUserInGroup(cnOfGroup, login);
	}

	public void addUserInGroup(String group, String userUid) {
		LdapConnection cnx = LdapConnection.getInstance();
		cnx.addUserInGroup(group, userUid);
		
	}
	public void removeGroup(String group){
		LdapConnection cnx = LdapConnection.getInstance();
		cnx.deleteGroup(group); 
	}
	public void addUser(Account account){
		LdapConnection cnx = LdapConnection.getInstance();
		cnx.addUser(account);
	}
	public void modifyGroup(String oldcn, String newcn, String description){
		LdapConnection cnx = LdapConnection.getInstance();
		cnx.modifyGroup(oldcn, newcn, description);
	}
	public List<Account> searchUsers(){
		LdapConnection cnx = LdapConnection.getInstance();
		return cnx.searchUsers();
	}
}
